package com.ply.flashsalessystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.entity.pojo.JionRepository;
import com.ply.flashsalessystem.entity.pojo.JionRepositoryDetails;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.vo.manage.JionRepositoryVo;
import com.ply.flashsalessystem.entity.vo.manage.JoinRepositoryDetailsVo;
import com.ply.flashsalessystem.entity.wrapper.JoinRepositoryDetailsWrapper;
import com.ply.flashsalessystem.entity.wrapper.JoinWrapper;
import com.ply.flashsalessystem.service.JionRepositoryDetailsService;
import com.ply.flashsalessystem.service.JionRepositoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@RestController
@RequestMapping("jionRepository")
@Api("入库接口")
@CrossOrigin
public class JionRepositoryController {
    @Autowired
    JionRepositoryService jionRepositoryService;
    @Autowired
    JionRepositoryDetailsService jionRepositoryDetailsService;

    /*
    入库操作  增删改查
     */
    @AuthorityName(authorityId = 8, authorityName = "增加入库记录")
    @ApiOperation("增加入库记录")
    @PostMapping("addRecord")
    public Result addRecord(@RequestBody List<JionRepositoryDetails> list) {
        return jionRepositoryService.addRepository(list)
                ? Result.ok().message("JoinRepository 成功")
                : Result.error().message("JoinRepository 失败");
    }

    @AuthorityName(authorityId = 9, authorityName = "查询入库记录")
    // 查询 入库记录
    @ApiOperation("查询入库记录")
    @PostMapping(value = {"getJoinRepository/{current}/{limit}", "getJoinRepository"})
    public Result getJoinRepository(@PathVariable(value = "current", required = false) Integer current,
                                    @PathVariable(value = "limit", required = false) Integer limit,
                                    @RequestBody(required = false) JoinWrapper joinWrapper) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }

        if (joinWrapper == null) {
            joinWrapper = new JoinWrapper();
        }
//        Page<JionRepository> jionRepositoryPage = jionRepositoryService.queryJoinRepository(current, limit, joinWrapper);
        Page<JionRepositoryVo> jionRepositoryPage = jionRepositoryService.queryJoinRepository(current, limit, joinWrapper);

        return Result.ok()
                .message("查询成功")
                .data("total", jionRepositoryPage.getTotal())
                .data("rows", jionRepositoryPage.getRecords());
    }

    // 查询 入库 详情 记录
    // 查询  入库记录
    @AuthorityName(authorityId = 10, authorityName = "查询入库详情记录")
    @ApiOperation("查询入库 详情 记录")
    @PostMapping(value = {"getJoinRepositoryDetails/{current}/{limit}","getJoinRepositoryDetails"})
    public Result getJoinRepositoryDetails(@PathVariable(value = "current",required = false) Integer current,
                                           @PathVariable(value = "limit",required = false) Integer limit,
                                           @RequestBody(required = false) JoinRepositoryDetailsWrapper joinRepositoryDetailsWrapper) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }

        if (joinRepositoryDetailsWrapper == null) {
            joinRepositoryDetailsWrapper = new JoinRepositoryDetailsWrapper();
        }
        Page<JoinRepositoryDetailsVo> joinRepositoryDetailsVoPage = jionRepositoryDetailsService.queryJoinRepositoryDetails(current, limit, joinRepositoryDetailsWrapper);
        return Result.ok()
                .message("查询成功")
                .data("total", joinRepositoryDetailsVoPage.getTotal())
                .data("rows", joinRepositoryDetailsVoPage.getRecords());
    }

    @AuthorityName(authorityId = 11, authorityName = "根据入库id查询入库详情记录")
    @ApiOperation("根据入库id 查询 入库详情记录")
    @PostMapping(value = {"getJoinRepositoryDetailsByJRId/{current}/{limit}/{jrId}","getJoinRepositoryDetailsByJRId/{jrId}"})
    public Result getJoinRepositoryDetailsByJRId(@PathVariable(value = "current",required = false) Integer current,
                                                 @PathVariable(value = "limit",required = false) Integer limit,
                                                 @PathVariable("jrId") Integer jrId) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        JoinRepositoryDetailsWrapper joinRepositoryDetailsWrapper = new JoinRepositoryDetailsWrapper();
        joinRepositoryDetailsWrapper.setJrId(jrId);
        Page<JoinRepositoryDetailsVo> joinRepositoryDetailsVoPage = jionRepositoryDetailsService.queryJoinRepositoryDetails(current, limit, joinRepositoryDetailsWrapper);
        return Result.ok()
                .message("查询成功")
                .data("total", joinRepositoryDetailsVoPage.getTotal())
                .data("rows", joinRepositoryDetailsVoPage.getRecords());
    }

}

